/********************************************************************************
* Name      : Stephen Durfey													*
* Class     : CS202, MWF 9:30am-10:20am											*
* Instructor: Ron Conway														*
* Class Code: 156																*
* File      : storelist.cpp														*
*																				*
* This program is designed to handle a simple employee database using a linked  *
* list. Each employee in the database is stored with the following information: *
* Employee Name, Employee ID number, the department the employee works in, and  *
* the amount of Sales the employee has made. The database will be constructed   *
* with the employees being listed in order, according their respective ID       *
* number.																		*
*																				*
* The program has a few various capabilities to handle the employee data.       *
* New employees can be added or deleted, and the database can be printed out    *
* in either its entirety, or by department code.								*
*																				*
* The following functions are included in the program and are coded in the		*
* storelist.cpp file. They include: insertNode(), deleteNode(), printList()     *
* and printDept().																*
*																				*
* Function Details:																*
*																				*
* 1) insertNode(Employee* &passedPtr)											*
*    This function is designed to add a node to the linked list. It accomplishes*
*    this by accepting the parameter passedPtr, which holds the address of the  *
*    node created within the main function, and assigning its address to the    *
*    pointer in the previous node. As the employee is added, the name and       *
*    employee ID number is printed out as confirmation that it was added to     *
*    the list.																	*
*																				*
* 2) deleteNode(int empID)														*
*    This function deletes the employee from the database, based on the         *
*    employee ID passed to the function as a parameter.							*
*																				*
* 3) printList()																*
*	 This function prints out the list in its entirety at the time of           *
*    execution.																	*
*																				*
* 4) printDept(int dNum)														*
*    This function prints out the list of employees by department, based on     *
*    the code received by the parameter dNum.									*
*																				*
********************************************************************************/



#include <string>
#include <iostream>
#include <iomanip>
#include "storelist.h"

using namespace std;

//This is the default constructor for the StoreList class. It assigns the
//head (the sole private data member of the class) to NULL.

StoreList::StoreList()
{
	head = NULL;
}

//This is the insertNode function that is detailed in general terms above. 

void StoreList::insertNode(Employee* &passedPtr)
{
	Employee* nodePtr, *previousPtr;

	//if the list is empty, assign head to the value passedPtr, which is received
	//in the function header. Once it has been added, it prints out the name and 
	//ID number of the newly added employee.

	if(head == NULL)
	{
		head = passedPtr;

		cout << "\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
		cout << "Employee " << passedPtr->ID << ", " << passedPtr->name << " has been added to the list\n";
		cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
	}
	else
	{
		previousPtr = nodePtr = head; //assigns all pointers to the beginning of the list

		//this if statement checks to see if the newly created node is being added
		//to the beginning of the list. if it is, then head is reassigned to the new
		//node and the new node is assigned to point to the previous list head. once
		//it has been added to the list, the employee data is printed out as confirmation.

		if(nodePtr->ID > passedPtr->ID)
		{
			head = passedPtr;
			passedPtr->nextEmployee = nodePtr;

			cout << "\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
			cout << "Employee " << passedPtr->ID << ", " << passedPtr->name << " has been added to the list\n";
			cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
		}
		else
		{

			//this if statement ensures that the list is sorted according to the Employee ID number.
			//it checks to make sure nodePtr is pointing to a node in the list AND if the employee
			//ID is lower than the ID of the passedPtr (received from the main function). Once an ID is
			//found that is higher than the passedPtr ID, the loop exits OR nodePtr has left the list
			//the loop exits. previousPtr is left pointing to the last node in the list OR the node
			//with an ID that is lower than the passedPtr->ID. The value of previousPtr->nextEmployee 
			//is set to equal the passedPtr, and passedPtr->nextEmployee is set to equal the next node
			//in the list, if there is one. The employee data is printed out as confirmation that
			//the employee was added to the list.

			while(nodePtr && (nodePtr->ID < passedPtr->ID))  
			{
				previousPtr = nodePtr;
				nodePtr = nodePtr->nextEmployee;
			}

			cout << "\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
			cout << "Employee " << passedPtr->ID << ", " << passedPtr->name << " has been added to the list\n";
			cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";

			previousPtr->nextEmployee = passedPtr;
			passedPtr->nextEmployee = nodePtr;
		}
	}
}

//This function is explained in general terms earlier in this file.

void StoreList::deleteNode(int empID)
{
	Employee* nodePtr, *previousPtr;
	nodePtr = head;

	//this if statement checks to see if the employee you wish to delete
	//is the list head of the linked list. If it is, then the head is reassigned
	//to the next node in the list, and the node is deleted.

	if(head && head->ID == empID)
	{
		head = head->nextEmployee;
		delete nodePtr;
	}
	else
	{
		//traverses the list until the employee ID is found, if it is, then the employee
		//data of the deleted employee is displayed as confirmation. if the employee is not
		//found in the list, then the user is informed the employee ID does not exist.

		while(nodePtr && nodePtr->ID != empID)
		{
			previousPtr = nodePtr;
			nodePtr = nodePtr->nextEmployee;
		}

		//checks to see if nodePtr is pointing to a node in the list. If it is,
		//then we know the employee ID was found. if it is not pointing to a node
		//in the list then we know the employee was not found.

		if(nodePtr)
		{
			previousPtr->nextEmployee = nodePtr->nextEmployee;

			cout << "\n--------------------------------------------------------------\n";
			cout << "Employee " << nodePtr->ID << ", " << nodePtr->name << " was deleted from the list!\n";
			cout << "--------------------------------------------------------------\n";

			delete nodePtr;
		}
		else
		{
			cout << "\n**************************************************\n";
			cout << "Employee " << empID << " was not found in the list\n";
			cout << "**************************************************\n";
		}
	}
}

//This function displays a nice header to make the output of the
//entire list easy to read.

void StoreList::printList()
{
	Employee* nodePtr;
	nodePtr = head;

	cout << endl << endl << setw(10) << left << "ID" << setw(20) << "Name"  << setw(16) << "Dept Number" << "Sales\n";
	cout << setw(10) << "==" << setw(20) << "====" << setw(16) << "===========" << "=====\n";

	//traverses the list and prints out the employee information

	while(nodePtr)
	{
		cout << setw(10) << nodePtr->ID << setw(20) << nodePtr->name << setw(16);
		cout << nodePtr->DeptNum << "$" << nodePtr->sales << endl;

		nodePtr = nodePtr->nextEmployee;
	}
}

//This function accepts the parameter dNum, which tells the function
//which department to print. In order to print out the proper header, a
//switch statement is used to determine the proper header, which is 
//determined by the value of dNum. 

void StoreList::printDept(int dNum)
{
	Employee* nodePtr;
	nodePtr = head;
	string deptName;

	switch(dNum)
	{
		case 111: deptName = "Video Dept";
				  break;
		case 222: deptName = "Audio Dept";
				  break;
		case 333: deptName = "Computer Dept";
			      break;
		case 444: deptName = "Appliance Dept";
			      break;
	};

	cout << "\n\n======================= " << deptName << " =======================\n\n";
	cout << setw(10) << left << "ID" << setw(20) << "Name"  << setw(16) << "Dept Number" << "Sales\n";
	cout << setw(10) << "==" << setw(20) << "====" << setw(16) << "===========" << "=====\n";

	//Traverses the list and prints out the employee data if their
	//department matches the value of dNum.

	while(nodePtr)
	{
		if(nodePtr->DeptNum == dNum)
		{
			cout << setw(10) << nodePtr->ID << setw(20) << nodePtr->name << setw(16);
			cout << nodePtr->DeptNum << "$" << nodePtr->sales << endl; 
		}

		nodePtr = nodePtr->nextEmployee;
	}
}

//This is the class destructor. It traverses the list and deletes each
//node as it moves along the list. 

StoreList::~StoreList()
{
	Employee* nodePtr, *previousPtr;

	nodePtr = head;
	previousPtr = head;

	while(nodePtr)
	{
		nodePtr = nodePtr->nextEmployee;
		delete previousPtr;
		previousPtr = nodePtr;
	}

	cout << "\n\nList has been destroyed!\n\n";
}














		
